<?php

/**
 * ECTouch Open Source Project
 * ============================================================================
 * Copyright (c) 2012-2014 http://ectouch.cn All rights reserved.
 * ----------------------------------------------------------------------------
 * 文件名称：MeetControoller.class.php
 * ----------------------------------------------------------------------------
 * 功能描述：活动控制器
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.ectouch.cn/docs/license.txt )
 * ----------------------------------------------------------------------------
 */
/* 访问控制 */
defined('IN_ECTOUCH') or die('Deny Access');

class MeetController extends CommonController {

    private $size = 10;
    private $page = 1;
    private $cat_id = 0;
    private $keywords = '';

    public function __construct() {
        parent::__construct();
        $this->cat_id = intval(I('get.id'));
    }

    /* ------------------------------------------------------ */

    //-- 活动分类
    /* ------------------------------------------------------ */
    public function index() {
        $cat_id = I('get.id', 0, 'intval');
        $sql = 'SELECT cat_id, cat_name' .
            ' FROM {pre}meet_cat ' .
            ' WHERE cat_type = 1 AND parent_id = '. $cat_id .
            ' ORDER BY sort_order ASC';
        $data = $this->model->query($sql);
        foreach($data as $key=>$vo){
            $data[$key]['url'] = url('meet_list', array('id'=>$vo['cat_id']));
        }
        $this->assign('meet_categories', $data); //活动分类树
        $this->display('meet_cat.dwt');
    }

    /* ------------------------------------------------------ */

    //-- 活动列表
    /* ------------------------------------------------------ */
    public function meet_list() {		
        $this->parameter();		
        $this->assign('keywords', $this->keywords);
        $this->assign('id', $this->cat_id);
		$meets_list = model('MeetBase')->get_cat_meets($this->cat_id, $this->page, $this->size, $this->keywords);
		$count = model('MeetBase')->get_meet_count($this->cat_id, $this->keywords);
        $this->pageLimit(url('meet_list', array('id' => $this->cat_id)), $this->size);
        $this->assign('pager', $this->pageShow($count));
        $this->assign('meets_list', $meets_list);
        //处理关键词描述
        $sql = "select * from ".M()->pre."meet_cat where cat_id = ".$this->cat_id;
        $cat = M()->query($sql);
        if (!empty($cat['0']['keywords'])) {
            $this->assign('meta_keywords',htmlspecialchars($cat['0']['keywords']));
        }
        if (!empty($cat['0']['cat_desc'])) {
            $this->assign('meta_description',htmlspecialchars($cat['0']['cat_desc']));
        }
        $this->display('meet_list.dwt');
    }

    /**
     * 活动列表异步加载
     */
    public function asynclist() {
        $this->parameter();
        $asyn_last = intval(I('post.last')) + 1;
        $this->size = I('post.amount');
        $this->page = ($asyn_last > 0) ? ceil($asyn_last / $this->size) : 1;
        $list = model('MeetBase')->get_cat_meets($this->cat_id, $this->page, $this->size, $this->keywords);
        $id = ($this->page - 1) * $this->size + 1;
        foreach ($list as $key => $value) {
            $this->assign('id', $id);
            $this->assign('meet', $value);
            $sayList [] = array(
                'single_item' => ECTouch::view()->fetch('library/asynclist_info.lbi')
            );
            $id++;
        }
        die(json_encode($sayList));
        exit();
    }

    /* ------------------------------------------------------ */
    //-- 活动详情
    /* ------------------------------------------------------ */
    public function info() {
		/* 微信分享设置 */
		$res = array();		
		$wxinfo   = model('Base')->model->table('wechat')->field('token, appid, appsecret')->find();
        $appid    = $wxinfo['appid'];
        $secret   = $wxinfo['appsecret'];
        
		//微信JS SDK
        $jssdk = new Jssdk($appid, $secret);
        $signPackage = $jssdk->GetSignPackage();

        $res['appid'] = $signPackage["appId"];
        $res['timestamp'] = $signPackage["timestamp"];
        $res['noncestr'] = $signPackage["nonceStr"];
        $res['signature'] = $signPackage["signature"];
		//
        $this->assign('res', $res);

        /* 活动详情 */
        $meet_id = intval(I('get.aid'));
        $meet = model('Meet')->get_meet_info($meet_id);

		$user_id=$meet['user_id'];
		//微信头像
		$wechat_pic= model('Base')->model->table('wechat_user')->field('headimgurl')->where(array('ect_uid'=>$user_id))->getOne();
        //后台添加的头像
		$user_pic= model('Base')->model->table('users')->field('user_pic')->where(array('user_id'=>$user_id))->getOne();
		

		/* 活动日期 */
		$meet['start_time'] = local_date('m月d日 H:i', $meet['start_time']);
		$meet['end_time'] = local_date('m月d日 H:i', $meet['end_time']);

        $this->assign('meet', $meet);
		$this->assign('wechat_pic', $wechat_pic);
        $this->assign('user_pic', $user_pic);

        /* 页面标题 */
        $page_info = get_page_title($meet['cat_id'], $meet['title']);
        $this->assign('page_title', htmlspecialchars($page_info['title']));
        /* meta */
        $this->assign('meta_keywords', htmlspecialchars($meet['keywords']));
        $this->assign('meta_description', htmlspecialchars($meet['description']));

		// 更新点击次数
        $data = 'click_count = click_count + 1';
        $this->model->table('meet')->data($data)->where('meet_id = ' . $meet_id)->update();

		//获得活动的评论
		$comments = model('Comment')->get_comment_info($meet_id, 2);
		$this->assign('comments', $comments);
		$comment_list = model('Comment')->get_comment($meet_id, 1,2);
        $this->assign('comment_list', $comment_list);

		//是否已经收藏
        $is_collect= model('Base')->model->table('collect')->field('rec_id')->where(array('user_id'=>$_SESSION['user_id'],'item_id'=>$meet_id,'item_type'=>2))->getOne();
        $this->assign('is_collect', $is_collect);
        $this->display('meet_info.dwt');
    }

	/**
	* 添加收藏
	*/
	public function collect_add(){
            $meet_id = intval(I('get.meet_id'));
            $data_i['item_type'] = 2;
            $data_i['item_id'] = $meet_id;
            $data_i['user_id'] = $_SESSION['user_id'];
            $data_i['add_time'] = gmtime();
            $this->model->table('collect')
                 ->data($data_i)
                 ->insert();
            show_message('收藏成功', '返回活动页面', 'index.php?c=meet&a=info&aid='.$meet_id, 'info');
	}

    /**
	* 取消收藏
	*/
	public function collect_del(){
            $meet_id = intval(I('get.meet_id'));            
			$where['user_id'] = $_SESSION['user_id'];
            $where['item_type'] = 2;
            $where['item_id'] = $meet_id;
            $this->model->table('collect')
                 ->where($where)
                 ->delete();
            show_message('取消成功', '返回活动页面', 'index.php?c=meet&a=info&aid='.$meet_id, 'info');
	}

	/**
	* 发表评论
	*/
	public function comment_add(){
		if (IS_POST) {
			$meet_id=I('post.meet_id');
            $data_i['comment_type'] = 2;
            $data_i['comment_rank'] = 1;
            $data_i['id_value'] = $meet_id;
            $data_i['user_id'] = $_SESSION['user_id'];
            $user_name=model('Base')->model->table('users')->field('user_name')->where(array('user_id'=>$_SESSION['user_id']))->getOne();
            $data_i['user_name'] = $user_name;
            $data_i['add_time'] = gmtime();
			$content=I('post.content');

			$content = strlen($content) > 100 ? mb_substr(htmlspecialchars($content), 0, 99) : htmlspecialchars($content);
            $data_i['content'] = $content;
            $this->model->table('comment')
                 ->data($data_i)
                 ->insert();
            show_message('发表成功,通过审核后才能显示。', '返回活动页面', 'index.php?c=meet&a=info&aid='.$meet_id, 'info');
		}
	}

	/**
     * 活动评论
     */
    public function comment_list()
    {
        $cmt = new stdClass();
        $cmt->id = !empty($_GET['id']) ? intval($_GET['id']) : 0;
        $cmt->type = !empty($_GET['type']) ? intval($_GET['type']) : 0;
        $cmt->page = isset($_GET['page']) && intval($_GET['page']) > 0 ? intval($_GET['page']) : 1;
        $com = model('Comment')->get_comment_info($cmt->id,0);
        $this->assign('comments_info', $com);
        $comment_type = 2;
        $size = I(C('page_size'), 10);
        $this->assign('show_asynclist', C('show_asynclist'));
        $count = $com['count'];
        $filter['page'] = '{page}';
        $offset = $this->pageLimit(url('meet/comment_list', $filter), $size);
        $offset_page = explode(',', $offset);
        $comment_list = model('Comment')->get_comment($cmt->id, $cmt->type, $comment_type, $offset_page[1], $offset_page[0]);
        $this->assign('comment_list', $comment_list);
        $this->assign('username', $_SESSION['user_name']);
        $this->assign('email', $_SESSION['email']);
        /* 验证码相关设置 */
        if ((intval(C('captcha')) & CAPTCHA_COMMENT) && gd_version() > 0) {
            $this->assign('enabled_captcha', 1);
            $this->assign('rand', mt_rand());
        }
        $result['message'] = C('comment_check') ? L('cmt_submit_wait') : L('cmt_submit_done');
        $this->assign('id', $cmt->id);
        $this->assign('type', $cmt->type);
        $this->assign('pager', $this->pageShow($count));
        $this->assign('title', '活动评论');
        $this->display('meet_comment_list.dwt');
    }
    
    /**
     * 处理参数便于搜索商品信息
     */
    private function parameter() {
        $this->assign('show_asynclist', C('show_asynclist'));
        // 如果分类ID为0，则返回总分类页
        $page_size = C('article_number');
        $this->size = intval($page_size) > 0 ? intval($page_size) : $this->size;
        $this->page = I('request.page') ? intval(I('request.page')) : 1;
        $this->cat_id = intval(I('request.id'));
        $this->keywords = I('request.keywords');
    }

}